package client.tablete.workspace;


import java.util.concurrent.ExecutionException;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ListActivity;

import android.content.DialogInterface;
import android.content.Intent;

import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;


public class ListTvs extends ListActivity{

    android.net.wifi.WifiManager.MulticastLock lock;
    //    android.os.Handler handler = new android.os.Handler();
	
//	TextView mSelectTxt;
	Button mSelectButton;
	private String[] tvs = null;
	private String nameTv;
	private String ipTv;
	private String portTv;
	@Override
	public void onCreate(Bundle icicle){
		super.onCreate(icicle);
		setContentView(R.layout.main);
		
		android.net.wifi.WifiManager wifi = (android.net.wifi.WifiManager) getSystemService(android.content.Context.WIFI_SERVICE);
		lock = wifi.createMulticastLock("mylockthereturn");
		lock.setReferenceCounted(true);
		lock.acquire();
		
		Listener listener = new Listener();
		listener.execute();

		try {
			tvs = listener.get();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		int layoutId = android.R.layout.simple_list_item_1;
		ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(this, layoutId, tvs);
		
		setListAdapter(myAdapter);
		
		registerForContextMenu(getListView());
	}
	
//	@Override
//	protected void onProgressUpdate(Integer... values){
//		super.onProgressUpdate(values);
//		
//		int progressBarValue = values[0].intValue();
//	     
//		// update progress bar:
//		mUpdateForecastProgressDialog.setProgress(progressBarValue);		
//	}
	

	private static final int CONNECTION_YES_NO_MESSAGE = 1;
	@Override
	protected Dialog onCreateDialog(int aRequestedDialaogId){
		switch(aRequestedDialaogId){
		case CONNECTION_YES_NO_MESSAGE:
				return new AlertDialog.Builder(ListTvs.this)
							.setIcon(R.drawable.little_tv)
							.setTitle(R.string.dialog_ask_connection)
							.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
								
								@Override 	// Option YES
								public void onClick(DialogInterface dialog, int which) {

									// Passing IP to CommunicationActivity
									Bundle commBundle = new Bundle();
									commBundle.putString("ip", ipTv);
									commBundle.putString("port", portTv);

									Intent intentCommunication = new Intent(ListTvs.this, CommunicationActivity.class);
									intentCommunication.putExtras(commBundle);
									ListTvs.this.startActivity(intentCommunication);
								}
							})
							.setNegativeButton("No", new DialogInterface.OnClickListener() {
								
								@Override	// Option NO
								public void onClick(DialogInterface dialog, int which) {
									// TODO Auto-generated method stub
									
								}
							})
							.create();
		default:
				break;
					
		}
		return null;
	}
	
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id){
//		mSelectTxt.setText("Selection: "+tvs[position]);
		int start = 0;
		int end = tvs[position].indexOf("=");
		this.nameTv = tvs[position].substring(0, end-1);
		Log.i("nameTv:", this.nameTv);
		start = tvs[position].indexOf("/", start) ;
		end =tvs[position].indexOf(":", start);
		this.ipTv = tvs[position].substring(start+1,end);
		Log.i("ipTv:", this.ipTv);
		start = end + 1;
		this.portTv = tvs[position].substring(start);
		Log.i("portTv:", this.portTv);

		showDialog(CONNECTION_YES_NO_MESSAGE);
		
	}
	
	
	@Override
	public boolean onContextItemSelected(MenuItem item){
		if(item.getItemId()==1){
			AdapterContextMenuInfo adapterMenuInfo = (AdapterContextMenuInfo) item.getMenuInfo();
//			mSelectTxt.setText("MenuSelection: "+tvs[adapterMenuInfo.position]);
			
		}
		return super.onContextItemSelected(item);
	}
	
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo){
		super.onCreateContextMenu(menu, v, menuInfo);
		menu.setHeaderTitle("Context Menu");
		menu.add(Menu.NONE, 1, Menu.NONE, "Menu choix1");
	}
	

	
}
